<template>
  <div id="userinfo">
    <Row class="restand">
      <i-col class="standkepudigndan" span="17">
        <div class="standardt">
          用户已购买标准
        </div>
          <Row class="knowledge">
            <!-- <i-col span="12">
              <span class="conditioninputitle">标准号</span>
              <Input v-model="ssnum" placeholder="请输入标准号" class="conditioninpu" />
            </i-col> -->
            <i-col span="8">
              <span class="conditioninputitle">标准号</span>
              <Input v-model="psn" placeholder="请输入标准号" class="conditioninpu" />
            </i-col>
            <i-col span="2" class="kwdtj">
              <Button class="searchbutton" type="success" v-on:click="getSearchResult()">检索</Button>
            </i-col>
        </Row>
        <div class="result">
          <Row class="orderrow" v-for="(item,index) in results" :key="index">
            <i-col  span="20" >
              <div  class="itemics">
                  标准号:<span class="itemicss" @click="getDetail(item.product_id)" v-html="item.productSn"></span>
                  <span class="itemicssd">标准名称:</span>
                  <span class="itemcsss">{{ item.productName }}</span>
                  <Button class="downbtn" :size="small" type="primary" @click="downpdf(item.product_id)">下载</Button>
              </div>
            </i-col>
          </Row>
          <Page :total="dataCount" :page-size="pageSize" show-total @on-change="changepage" v-if="showPage" class-name="changepage" show-elevator />
        </div>
      </i-col>
    </Row>
  </div>
</template>

<script>
import {neturl} from '../assets/js/common'
export default {
  name: 'seniorsearch',
  data () {
    return {
      msg: '内蒙古标准文献服务平台',
      theme1: 'light',
      byname: 'bymenu',
      value: '',
      results: [],
      model1: '',
      keyword: '',
      dataCount: 0,
      pageSize: 10,
      pageNum: 1,
      showPage: false,
      userinfo: '',
      islogin: false,
      islog: true,
      username: '',
      tatolDeposit: '',
      tatolPoint: '',
      psn: ''
    }
  },
  methods: {
    getSearchResult () {
      let uploadcc = { data: '{page: ' + this.pageNum + ', pagesize: ' + this.pageSize + ', uid:"' + this.userinfo.userId + '", psn:"' + this.psn + '"}' }
      this.$axios.post(neturl + '/by/order/getPurchasedStandardByUserId?token=' + this.userinfo.token + '&loginMark=' + this.userinfo.loginMark, uploadcc
      ).then(res => {
        // console.log(res)
        this.results = res.data.data.result
        this.dataCount = res.data.data.records
        this.showPage = true
        this.$Spin.hide()
      })
    },
    changepage (value) {
      this.pageNum = value
      this.getSearchResult()
    },
    getDetail (id) {
      console.log(id)
      let routeData = this.$router.resolve({path: '/standdetail', query: {id: id, keyword: ''}})
      window.open(routeData.href, '_blank')
    },
    downpdf (idd) {
      this.$Spin.show()
      let upload = { data: idd }
      // console.log(upload)
      this.$axios.post(neturl + '/by/standard/downbyid?token=' + this.userinfo.token + '&loginMark=' + this.userinfo.loginMark, upload).then(res => {
        console.log(res)
        // var a = document.createElement('a')
        // a.download = '标准'
        // a.href = 'http://' + res.result
        // a.target = '_blank'
        // // a.setAttribute('id', 'export')
        // document.getElementById('test').append(a) // 修复firefox中无法触发click
        // a.click()
        // 创建a标签
        // console.log(res.data.data.info)
        // console.log(res.data.data.result)
        if (res.data.code === 200) {
          const link = document.createElement('a')
          // download属性
          link.setAttribute('download', res.data.data.info)
          link.setAttribute('target', '_blank')
          // href链接
          link.setAttribute('href', 'http://' + res.data.data.result)
          // 自执行点击事件
          link.click()
          this.$Spin.hide()
        } else {
          this.$Spin.hide()
          this.$Message.error('抱歉，当前标准正在收录中')
        }
        // document.body.removeChild(link)
        // let URL = this.dataURLtoBlob(res.data.data.result)
        // var reader = new FileReader()
        // reader.readAsDataURL(URL)
        // reader.onload = function (e) {
        //   // 兼容IE
        //   if (window.navigator.msSaveOrOpenBlob) {
        //     var bstr = atob(e.target.result.split(',')[1])
        //     var n = bstr.length
        //     var u8arr = new Uint8Array(n)
        //     while (n--) {
        //       u8arr[n] = bstr.charCodeAt(n)
        //     }
        //     var blob = new Blob([u8arr])
        //     window.navigator.msSaveOrOpenBlob(blob, res.data.data.info)
        //   } else {
        //     // 转换完成，创建一个a标签用于下载
        //     var a = document.createElement('a')
        //     a.download = res.data.data.info
        //     a.href = e.target.result
        //     a.setAttribute('id', 'export')
        //     document.getElementById('purchaseManage').append(a) // 修复firefox中无法触发click
        //     a.click()
        //     // document.getElementById('export').remove()
        //   }
        // }
      })
    }
  },
  created () {
    this.$Spin.show()
    // this.model1 = this.cityList[0].value
    this.userinfo = this.$cookie.get('user')
    if (this.userinfo === null) {
      this.$router.push('/login')
    }
    this.userinfo = JSON.parse(this.userinfo)
    // 已登录
    if (this.userinfo != null) {
      this.islogin = true
      this.islog = false
      this.username = this.userinfo.realName
    }
    this.getSearchResult()
  }
}
</script>
<style scoped>
  @import '../assets/css/purchasedstandard.css';
</style>
